package com.peng.model;

import com.fasterxml.jackson.annotation.JsonIgnore;
import lombok.Data;
import lombok.Getter;
import lombok.Setter;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.util.ObjectUtils;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import java.util.List;

@Getter
@Setter
public class TUserDetailsImpl extends TUser implements UserDetails, Serializable {
    private static final long serialVersionUID = 1L;

    // 由于JSON转换需要无参构造器，所以必须调用父类无参构造函数（在无@Data注解下）
    public TUserDetailsImpl() {
        super();
    }

    public TUserDetailsImpl(TUser user) {
        super(user);
    }

    @JsonIgnore
    public TUser getUser() {
        return new TUser(
                super.getId(),
                super.getLogin_act(),
                super.getLogin_pwd(),
                super.getName(),
                super.getPhone(),
                super.getEmail(),
                super.getAccount_no_expired(),
                super.getCredentials_no_expired(),
                super.getAccount_no_locked(),
                super.getAccount_enabled(),
                super.getCreate_time(),
                super.getCreate_by(),
                super.getEdit_time(),
                super.getEdit_by(),
                super.getLast_login_time()
        );
    }
    //-------------------------实现Spring Security中UserDetails接口的7个方法------------------------------
    /**
     * 角色List
     */
    private List<String> roleList;

    /**
     * 权限标识符List
     */
    private List<String> permissionList;

    /**
     * 菜单的List
     */
    private List<TPermission> menuPermissionList;

    @JsonIgnore
    @Override
    public Collection<? extends GrantedAuthority> getAuthorities() {
        List<GrantedAuthority> list = new ArrayList<>();
        //角色
        if (!ObjectUtils.isEmpty(this.getRoleList()))  {
            this.getRoleList().forEach(role -> {
                list.add(new SimpleGrantedAuthority(role));
            });
        }
        if (!ObjectUtils.isEmpty(this.getPermissionList()))  {
            //权限标识符
            this.getPermissionList().forEach(permission -> {
                list.add(new SimpleGrantedAuthority(permission));
            });
        }
        return list;
    }

    @JsonIgnore
    @Override
    public String getPassword() {
        return super.getLogin_pwd();
    }

    @JsonIgnore
    @Override
    public String getUsername() {
        return super.getLogin_act();
    }

    @JsonIgnore
    @Override
    public boolean isAccountNonExpired() {
        return super.getAccount_no_expired() == 1;
    }

    @JsonIgnore
    @Override
    public boolean isAccountNonLocked() {
        return super.getAccount_no_locked() == 1;
    }

    @JsonIgnore
    @Override
    public boolean isCredentialsNonExpired() {
        return super.getCredentials_no_expired() == 1;
    }

    @JsonIgnore
    @Override
    public boolean isEnabled() {
        return super.getAccount_enabled() == 1;
    }

}
